package com.ziyun.pop.web.modules.quotePrintFactory.controller;

import com.utility.JsonUtils;
import com.utility.http.HttpUtils;
import com.ziyun.pop.common.utils.UserUtils;
import com.ziyun.pop.modules.quotePrintFactory.vo.QuotePrintFactoryEntityVo;
import com.ziyun.pop.modules.quotePrintFactoryPrice.entity.QuotePrintFactoryPriceEntity;
import io.renren.common.annotation.SysLog;
import io.renren.common.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.jws.soap.SOAPBinding;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 工厂印刷机配置表
 *
 * @author llc
 * @email 
 * @date 2018-09-07 09:32:19
 */
@RestController
@RequestMapping("quotePrintFactory/quoteprintfactory")
public class QuotePrintFactoryController {
    private  static Logger logger = LoggerFactory.getLogger(QuotePrintFactoryController.class);

    @Value("${erpApi}")
    private String erpApi ;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("quotePrintFactory:quoteprintfactory:list")
    public R list(@RequestParam Map<String, Object> params){
        // 获取当前用户的商户id
        Long sellerId =  UserUtils.getCurentUser().getSellerId();
        params.put("factoryId", sellerId);
        params.put("printType","shangwuyinshua");
        String url = erpApi + "/quotePrintFactory/quoteprintfactory/list";
        logger.info("==============请求url："+url+" ================");
        logger.info("==============请求参数："+params+" ================");
        String response =  HttpUtils.doPost(url,params);
        logger.info("==============请求响应："+response+" ================");
        R rs = JsonUtils.string2Object(response, R.class);

        return rs;
    }



    /**
     * 返回工厂以及配价信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("quotePrintFactory:quoteprintfactory:info")
    public R info(@PathVariable("id") Integer id){
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("id", id);
        String url = erpApi + "/quotePrintFactory/quoteprintfactory/info";
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);

        return rs;
    }




    /**
     * 删除
     */
    @SysLog("工厂印刷机删除")
    @RequestMapping("/delete")
    //@RequiresPermissions("quotePrintFactory:quoteprintfactory:delete")
    public R delete(@RequestBody Integer[] ids){
        //删除quote_print_factory表中数据，修改表中的del_flag字段
        //创建一个Map封装修改的参数

        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("ids", ids);
        paramBodyMap.put("update_user",UserUtils.getCurentUser().getUsername());

        String url = erpApi + "/quotePrintFactory/quoteprintfactory/delete";
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);

        return rs;
    }

    /**
     * 保存打印机及其价格
     */
    @SysLog("保存工厂印刷机及其价格")
    @RequestMapping("/saveWithPrice")
    //@RequiresPermissions("quotePrintFactory:quoteprintfactory:save")
    public R savePrintFacotoryWithPrintPrice(@RequestBody QuotePrintFactoryEntityVo quotePrintFactoryVo){
        Long factoryId =  UserUtils.getCurentUser().getSellerId();
        quotePrintFactoryVo.getQuotePrintFactory().setFactoryId(Integer.valueOf(factoryId.toString()));
        String url = erpApi + "/quotePrintFactory/quoteprintfactory/saveWithPrice";
        String createName=UserUtils.getCurentUser().getUsername();
        Map<String ,Object> map=new HashMap<String ,Object>();
        map.put("quotePrintFactoryVo",JsonUtils.object2String(quotePrintFactoryVo));
        map.put("createName",createName);
        String response =  HttpUtils.doPost(url,map);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }

    /**
     * 修改打印机及其价格
     */
    @SysLog("修改工厂印刷机及其价格")
    @RequestMapping("/updateWithPrice")
    //@RequiresPermissions("quotePrintFactory:quoteprintfactory:update")
    public R update(@RequestBody QuotePrintFactoryEntityVo quotePrintFactoryVo){
        Long factoryId =  UserUtils.getCurentUser().getSellerId();
        quotePrintFactoryVo.getQuotePrintFactory().setFactoryId(Integer.valueOf(factoryId.toString()));
        List<QuotePrintFactoryPriceEntity> quotePrintFacotoryPirceList = quotePrintFactoryVo.getQuotePrintFacotoryPirceList();
        for(int i=0;i<quotePrintFacotoryPirceList.size();i++){
            QuotePrintFactoryPriceEntity quotePrintFactoryPriceEntity=quotePrintFacotoryPirceList.get(i);
            quotePrintFactoryPriceEntity.setCreateDate(new Date());
            quotePrintFactoryPriceEntity.setCreateUser(UserUtils.getCurentUser().getUsername());
        }
        String url = erpApi + "/quotePrintFactory/quoteprintfactory/updateWithPrice";
        String response =  HttpUtils.doPost(url,quotePrintFactoryVo);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }

    /**
     * 修改打印机是否前台展示状态
     */
    @SysLog("修改工厂印刷机否前台展示状态")
    @RequestMapping("/updateDisplay/{id}")
    //@RequiresPermissions("quotePrintFactory:quoteprintfactory:update")
    public R updateDisplay(@PathVariable("id") Integer id){
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("id", id);
        String url = erpApi + "/quotePrintFactory/quoteprintfactory/updateDisplay";
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }
}
